package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;

    /**
     * 条件分页查询员工信息
     * @param param
     * @return PageResult<Emp>
     */
    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        //排序方式
        page.addOrder(OrderItem.desc("update_time"));
        //分页条件查询
//        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName,param.getName())
//                .eq(param.getGender() != null,Emp::getGender,param.getGender())
//                .between(param.getBegin() != null && param.getBegin() != null, Emp::getEntryDate, param.getBegin(), param.getEnd());
//        page = page(page, queryWrapper);

        page = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(),Emp::getName,param.getName())
                .eq(param.getGender() != null,Emp::getGender,param.getGender())
                .between(param.getBegin() != null && param.getBegin() != null, Emp::getEntryDate, param.getBegin(), param.getEnd())
                .page(page);
        //获取员工部门名称
        List<Emp> emps = page.getRecords();
        List<Integer> deptIds = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        if (!deptIds.isEmpty()) {
            List<Dept> depts = deptService.listByIds(deptIds);
            emps.forEach(emp -> {
                depts.forEach(dept -> {
                    if (emp.getDeptId() != null && emp.getDeptId() == dept.getId()) {
                        emp.setDeptName(dept.getName());
                    }
                });
            });
        }
        PageResult<Emp> pageResult = new PageResult<>(page.getTotal(), emps);
        return pageResult;
    }

    /**
     * 新增员工
     *
     * @param emp
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
        //新增工作经历
        List<EmpExpr> empExprList = emp.getExprList();
        if (empExprList != null && !empExprList.isEmpty()) {
            empExprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprService.saveBatch(empExprList);
        }
    }

    /**
     * 修改员工
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmp(Emp emp) {
        //1.更新员工表
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        //2.更新工作经历表
        //先删除
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        //再新增
        List<EmpExpr> empExprList = emp.getExprList();
        if (empExprList != null && !empExprList.isEmpty()) {
            empExprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprService.saveBatch(empExprList);
        }
    }

    /**
     * 根据ID获取员工信息
     *
     * @param id
     * @return
     */
    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }

    /**
     * 批量删除员工
     *
     * @param ids
     */
    @Override
    public void deleteEmpByIds(List<Integer> ids) {
        //删除员工表
        removeBatchByIds(ids);
        //删除工作经历表
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }
}
